package com.wzh.bargain.service.impl;

import com.wzh.bargain.vo.OrderVo;
import com.wzh.common.utils.R;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.bargain.dao.OrderBargainDao;
import com.wzh.bargain.entity.OrderBargainEntity;
import com.wzh.bargain.service.OrderBargainService;


@Service("orderBargainService")
public class OrderBargainServiceImpl extends ServiceImpl<OrderBargainDao, OrderBargainEntity> implements OrderBargainService {

    @Autowired
    private OrderBargainDao orderBargainDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userId = (String) params.get("userId");
        IPage<OrderBargainEntity> page = this.page(
                new Query<OrderBargainEntity>().getPage(params),
                new QueryWrapper<OrderBargainEntity>().eq("user_id",userId)
        );

        return new PageUtils(page);
    }

    @Override
    public R addOrder(OrderVo orderVo) {
        //判断是否是限定一个
        Integer count = orderBargainDao.getUserCountById(orderVo.getUserId(), orderVo.getActivityId());
        if (count>=1){
            return R.error("一个用户只能参加一次活动");
        }else {
            //mq异步生成订单
            rabbitTemplate.convertAndSend("msg3",orderVo);
            return R.ok("初始化订单完成");
        }
    }

}